package cn.fancy.sdk.entity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import cn.fancy.sdk.common.utils.DevUtil;
import cn.fancy.sdk.common.utils.NetworkImpl;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 设备基本参数
 */
public class DevBase implements JsonParseInterface {
    
    private final String _DevBase_Jason_Name = "DevBase";
    
    private final String _firstImsi = "a";
    private final String _gameVerName = "c";
    private final String _gameVer = "d";
    private final String _networkInfo = "e";
    private final String _projectId = "f";
    private final String _packageName = "g";
    private final String _sdkVersion = "h";
    private final String _deviceParams = "i";
    private final String _currentImsi = "j";
    private final String _iccId = "k";
    private final String _mac = "l";
    private final String _phoneMod = "n";
    private final String _os = "o";
    private final String _devEnv = "p";
    private final String _gameName = "q";
    
    /**
     * Sim卡序列号
     */
    private String firstImsi;
    
    /**
     * 游戏版本名称
     */
    private String gameVerName;
    
    
    /**
     * 游戏版本号
     */
    private int gameVerCode;
    
    
    
    /**
     * 网络类型
     */
    private String networkInfo;
    
    
    /**
     * 项目ID
     */
    private String projectId;
    
    /**
     * 游戏包名
     */
    private String packageName;
    
    /**
     * 游戏名
     */
    private String gameName;
    
    /**
     * sdk协议版本号
     */
    private int sdkVersion;
    
    /**
     * 自定义手机唯一标识
     */
    private String deviceParams;
    
    
    /**
     * 当前imsi
     */
    private String currentImsi;
    
    
    /**
     * 客户端识别地域的标示
     */
    private String iccId;
    
    /**
     * 手机Mac地址
     */
    private String mac;
    
    /**
     * 手机型号
     */
    private String phoneMod;
    
    
    /**
     * 手机操作系统
     */
    private String os;
    
    
    private int devEnv;
    
    
    public DevBase(Context ctx) {
        
        currentImsi = "";
        
        firstImsi = DevUtil.getIMSI(ctx);
        if (firstImsi == null || firstImsi.equals("")) {
            firstImsi = "";
        }
        iccId = "default";
        
        packageName = ctx.getPackageName();
        
        if (packageName == null || packageName.equals("")) {
            packageName = "";
        }
        
        sdkVersion = SdkBaseInfo.sdkVersion;
        
        PackageManager pm = ctx.getPackageManager();
        PackageInfo info;
        try {
            info = pm.getPackageInfo(ctx.getPackageName(), 0);
            gameVerName = info.versionName;
            gameVerCode = info.versionCode;
            gameName = (String) pm.getApplicationLabel(pm.getApplicationInfo(ctx.getPackageName(), 0));
        } catch (NameNotFoundException e1) {
            
            gameVerName = "";
            gameVerCode = 0;
            gameName = "";
        }
        
        if (gameVerName == null || "".equals(gameVerName)) {
            gameVerName = "";
        }
        
        networkInfo = NetworkImpl.getNetworkTypeName(ctx);
        if (networkInfo == null) {
            networkInfo = "unknown";
        } else {
            if (NetworkImpl.isWifiNetWork(ctx)) {
                networkInfo = "wifi_" + networkInfo;
            }
        }
        
        projectId = DevUtil.getProjectId(ctx).trim();
        
        phoneMod = Build.MODEL;
        if (phoneMod == null || "".equals(phoneMod)) {
            phoneMod = "";
        }
        
        os = "Android_" + Build.VERSION.RELEASE;
        mac = "";
        deviceParams = "";
        
        devEnv = DevEnv.getDevEnv();
        
    }
    
    @Override
    public String toString() {
        return "DevBase [imsi=" + firstImsi + ", gameVerName=" + gameVerName + ", gameVerCode=" + gameVerCode
            + ", networkInfo=" + networkInfo + ", projectId="
            + projectId + ", packageName=" + packageName + ",gameName" + gameName
            + ", sdkVersion=" + sdkVersion
            + ", deviceParams=" + deviceParams
            + ", currentImsi=" + currentImsi + ", iccId=" + iccId
            + ", mac=" + mac + ", phoneMod=" + phoneMod + ", os=" + os + ",devEnv=" + devEnv
            + "]";
    }
    
    
    @Override
    public JSONObject buildJson() {
        
        try {
            JSONObject json = new JSONObject();
            json.put(_firstImsi, firstImsi);
            json.put(_gameVerName, gameVerName);
            json.put(_gameVer, gameVerCode);
            json.put(_networkInfo, networkInfo);
            json.put(_projectId, projectId);
            json.put(_packageName, packageName);
            json.put(_sdkVersion, sdkVersion);
            json.put(_deviceParams, deviceParams);
            json.put(_currentImsi, currentImsi);
            json.put(_iccId, iccId);
            json.put(_mac, mac);
            json.put(_phoneMod, phoneMod);
            json.put(_os, os);
            json.put(_devEnv, devEnv);
            json.put(_gameName, gameName);
            
            return json;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    @Override
    public void parseJson(JSONObject json) {
    }
    
    @Override
    public String getShortName() {
        return _DevBase_Jason_Name;
    }
    
    
    /**
     * 单个SDK的相关配置信息，该配置信息需要在出包前，按需求进行配置
     *
     * @author lu.xing
     */
    public static class DevEnv {
        
        /**
         * 此字段不传服务器，仅做客户端标识
         */
        //title type: 0->无    1->有
        public static int LOGO_BAR = 1;
        
        
        
        
        /**
         * 0x0001为横屏，0x0000为竖屏
         */
        public static int ORI = 0x0000;
        
        /**
         * 带LOGO 为0x0002，不带LOGO为0
         */
        public static int LOGO = 0x0001;
        
        
        
        /**
         * 有登录模块为0x0004,没有登录模块为0
         */
        public static int LOGIN_MODEL = 0x0004;
        /**
         * 是否支持短信支付，0为不支持
         */
        public static int SMS_PAY = 0x0008;
        /**
         * 是否支持支付宝，0为不支持
         */
        public static int ALI_PAY = 0x0010;
        /**
         * 是否支持财富通，0为不支持
         */
        public static int TENCENT_PAY = 0x0020;
        
        /**
         * 是否支持微信，0为不支持     0x0200支持
         */
        
        public static int WEIXIN_PAY = 0x0200; //SDK客户端支持微信支付
        
        
        /**
         * 是否支持运营商SDK-MM，0为不支持
         */
        
        public static int CM_MM_PAY = 0x0000;
        
        /**
         * 是否支持运营商SDK-沃商店，0为不支持
         */
        public static int UNICOM_WO_PAY = 0;
        
        
        
        /**
         * 是否支持运营商SDK-天翼空间，0为不支持
         */
        public static int TY_PAY = 0x00;
        /**
         * 是否支持运营商SDK-爱游戏，0为不支持
         */
        public static int AYX_PAY = 0x0000;
        /*
         * QQlogin
         */
        public static int QQ_LOGIN = 0x01 << 14;
        /*
         * SINA login
         */
        public static int SINA_LOGIN = 0x01 << 15;
        
        
        public static int getDevEnv() {
            return ORI | LOGO | LOGIN_MODEL | SMS_PAY | ALI_PAY | TENCENT_PAY
                | WEIXIN_PAY | CM_MM_PAY | UNICOM_WO_PAY | TY_PAY | AYX_PAY | QQ_LOGIN | SINA_LOGIN;
        }
    }
    
}
